package util;

import java.io.FileOutputStream;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

import java.util.ArrayList;
import java.util.Date;

import com.itextpdf.text.Anchor;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chapter;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Section;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

public class PdfFilePrint
{
	private static Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18, Font.BOLD);
	private static Font redFont = new Font(Font.FontFamily.TIMES_ROMAN, 12,	Font.NORMAL, BaseColor.RED);
	//private static Font subFont = new Font(Font.FontFamily.TIMES_ROMAN, 16,	Font.BOLD);
	private static Font smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD);

	public boolean PDFPrint(String title, String subject, String keywords, String maker, String[] columns, ArrayList<ArrayList<String>> list)
	{		
		Document document = new Document();
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
		Date date = new Date();

		try
		{
			PdfWriter.getInstance(document, new FileOutputStream("C:/" + title + "_" + dateFormat.format(date) + ".pdf"));
			document.open();
			addMeta(document, title, subject, keywords, maker);
			addFrontPage(document, title, maker);
			addContent(document, title, columns, list);

			document.close();
			
			return true;
		} 
		catch (Exception e)
		{
			e.printStackTrace();
		}

		return false; 
	}

	private static void addMeta(Document document, String title, String subject, String keywords, String maker)
	{
		document.addTitle(title);
		document.addSubject(subject);
		document.addKeywords(keywords);
		document.addAuthor(maker);
		document.addCreator(maker);
	}

	private static void addFrontPage(Document document, String title, String maker) throws DocumentException
	{
		Paragraph preface = new Paragraph();

		addEmptyLine(preface, 1);

		preface.add(new Paragraph(title, catFont));

		addEmptyLine(preface, 1);

		preface.add(new Paragraph("Report generated by: " + maker + " on " + new Date(), smallBold));

		addEmptyLine(preface, 30);

		preface.add(new Paragraph("This document is a preliminary version and should not be taken as the finished product", redFont));

		document.add(preface);
		document.newPage();
	}

	private static void addContent(Document document, String title, String columns[], ArrayList<ArrayList<String>> list) throws DocumentException
	{
		Anchor anchor = new Anchor(title, catFont);
		anchor.setName(title);

		Chapter catPart = new Chapter(new Paragraph(anchor), 1);

		Paragraph subPara = new Paragraph();
		Section subPart = catPart.addSection(subPara);

		createContentTable(subPart, columns, list);

		document.add(catPart);
	}

	private static void createContentTable(Section section, String[] columns, ArrayList<ArrayList<String>> list)
	{
		PdfPTable table = new PdfPTable(columns.length);

		PdfPCell c1;

		for(String obj : columns)
		{
			c1 = new PdfPCell(new Phrase(obj));
			c1.setHorizontalAlignment(Element.ALIGN_CENTER);

			table.addCell(c1);
		}

		table.setHeaderRows(1);

		for(int i = 0; i<list.get(0).size(); i++)
		{
			for (int k = 0; k < list.size(); k++)
			{
				table.addCell(list.get(k).get(i));
			}
		}

		section.add(table);
	}

	private static void addEmptyLine(Paragraph paragraph, int number)
	{
		for (int i = 0; i < number ; i++)
		{
			paragraph.add(new Paragraph(" "));
		}
	}
}